package com.nx.qwr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * @Author: Qingwr
 * @Date: 2022/8/16 15:37
 * @Description 电话号码分区类
 *   需求: 手机号码分区,手机号码为第二个字段
 * 	 135 开头放一个文件
 * 	 136 开头放一个文件
 * 	 137 开头放一个文件
 * 	 其他开头   放一个文件
 *
 */
public class JobMain2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration entries = new Configuration();
        Job job = Job.getInstance(entries, "pationMain");

        //数据源
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path(args[0]));

        //设置Mapper
        job.setMapperClass(PFMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        //设置分区
        job.setPartitionerClass(PFPationer.class);
        //需要大于等于PFPationer.getPartition()人中return的种数
        job.setNumReduceTasks(5);

        //设置reducer
        job.setReducerClass(PFReduer.class);
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        //输出路径
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job, new Path(args[1]));


        boolean b = job.waitForCompletion(true);
        System.out.println(b ? 0 : 1);


    }
}
